//
//  RGXHistoryAwardController.m
//  JiCaiBao
//
//  Created by rgx on 16/11/7.
//  Copyright © 2016年 rgx. All rights reserved.
//

#import "RGXHistoryAwardController.h"
#import "MJRefresh.h"
#import "awardData.h"
#import "AwardCell.h"
#import "RGXAwardDetailController.h"
@interface RGXHistoryAwardController (){

    NSMutableArray *orderData;
    BOOL isFlash;
}

@end

@implementation RGXHistoryAwardController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *title = @"";
    if ([self.gameId isEqualToString:lottery_ssq_id]) {
        title = @"双色球";
    }else if ([self.gameId isEqualToString:lottery_fc3d_id]){
        title = @"福彩3D";
    }else if ([self.gameId isEqualToString:lottery_hbk3_id]){
        title = @"快3";
    }else if ([self.gameId isEqualToString:lottery_qlc_id]){
        title = @"七乐彩";
    }
    [self setShowTitle:title];
    [self initLeftNavigationBarItem];
    
    self.historyAwardTB.delegate = self;
    self.historyAwardTB.dataSource = self;
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.historyAwardTB.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    // 马上进入刷新状态
    [self.historyAwardTB.mj_header beginRefreshing];
    
    self.historyAwardTB.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadNewData{
    isFlash = YES;
    [self QueryPrizesList];
}

- (void)loadMoreData
{
    isFlash = NO;
    [self QueryPrizesList];
    [self.historyAwardTB.mj_footer endRefreshing];
    
    NSLog(@"加载更多数据");
}

- (void)QueryPrizesList{
    
    __weak RGXHistoryAwardController *selfInfo = self;
    
    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    
    unsigned long tempPage = orderData.count ; // tempPage 最好是tableData.count
    NSInteger ifrom = tempPage;
    NSString *from = [NSString stringWithFormat:@"%ld",(long)ifrom];
    if (isFlash) {//从0开始
        from=@"0";
    }
    
    [body setObject:self.gameId forKey:@"gameId"];
    [body setObject:from forKey:@"from"];
    [body setObject:@"30" forKey:@"size"];
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    [manager POST:QueryPrizes parameters:body progress:^(NSProgress * uploadProgress) {
        
    } success:^(NSURLSessionDataTask * task, id responseObject) {
        
        [self.historyAwardTB.mj_header endRefreshing];

        // 请求成功
        if(responseObject){
            RGXLog(@"responseObject: 历史开奖信息= %@",responseObject);
            [selfInfo handlePrizeData:responseObject];
        } else {
            RGXLog(@"无数据");
        }

    } failure:^(NSURLSessionDataTask * task, NSError * error) {
        
        [self.historyAwardTB.mj_header endRefreshing];
        RGXLog(@"error = %@",error);
    }];

}

- (void)handlePrizeData:(NSDictionary *)data{
    
    if (![data isKindOfClass:[NSDictionary class]]) {
        return;
    }
    if (![[data objectForKey:@"statusCode"] isEqualToString:@"0000"]) {
        return;
    }
    NSArray *listArr = [[data objectForKey:@"body"] objectForKey:@"list"];
    
    if (listArr.count == 0) {
        return;
    }
    if (listArr.count == 0) {
        [self.historyAwardTB reloadData];
        return;
    }
    if (orderData && isFlash) {
        [orderData removeAllObjects];
    }else if(!orderData){
        orderData = [[NSMutableArray alloc]init];
    }
    for (NSDictionary *dict in listArr) {
        awardData *data = [[awardData alloc]init];
        data.lotteryTime = [self getTimeStr:[dict objectForKey:@"prizeTime"]];
        data.lotteryIssue = [NSString stringWithFormat:@"%@",[dict objectForKey:@"issueNo"]];
        data.prizeBalls = [dict objectForKey:@"prizeBalls"];
        [orderData addObject:data];
    }
    [self.historyAwardTB reloadData];
}
#pragma mark UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(orderData.count){
        
        tableView.separatorStyle = YES;

        return orderData.count;
    }else{
        tableView.separatorStyle = NO;
        return 0;
    }
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strid =@"awardCell";
    AwardCell *cell = [tableView dequeueReusableCellWithIdentifier:strid];
    if(cell ==nil){
        
        NSArray *arr = [[NSBundle mainBundle]loadNibNamed:@"AwardCell" owner:self options:nil];
        cell = [arr lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    awardData *data = [[awardData alloc]init];

    if (orderData) {
        data = [orderData objectAtIndex:indexPath.row];
        
    }
    if ([self.gameId isEqualToString:lottery_ssq_id]) {
        [cell flashCell:data :0];

    }else if ([self.gameId isEqualToString:lottery_hbk3_id]){
        cell.arrowHeadImg.hidden = YES;
        [cell flashCell:data :1];

    }else if ([self.gameId isEqualToString:lottery_fc3d_id]){
        [cell flashCell:data :2];

    }else if ([self.gameId isEqualToString:lottery_qlc_id]){
        [cell flashCell:data :3];

    }
    [cell flashCell1:self.gameId:(int)indexPath.row];
    
    //隐藏彩种名称，同时更改期次、开奖时间的约束
    cell.gameId.hidden = YES;
    cell.gameDesLeading.constant = -66;

    //隐藏没用的cell
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.0;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.gameId isEqualToString:lottery_hbk3_id]) {
        return;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    RGXAwardDetailController *awardDetail = [[RGXAwardDetailController alloc]initWithNibName:@"RGXAwardDetailController" bundle:nil];
    awardDetail.gameId = self.gameId;
    
    awardData *data = [[awardData alloc]init];
    
    if (orderData) {
        data = [orderData objectAtIndex:indexPath.row];
        
    }
    awardDetail.issueNo = [NSString stringWithFormat:@"%@",data.lotteryIssue];
    [self.navigationController pushViewController:awardDetail animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
